package com.ruoyi.project.wechatprogram.webreport.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wechatprogram.ordermaster.domain.WxOrderMaster;
import com.ruoyi.project.wechatprogram.ordermaster.service.IWxOrderMasterService;
import com.ruoyi.project.wechatprogram.webreport.domain.WxVisitData;
import com.ruoyi.project.wechatprogram.webreport.service.IWxVisitDataService;
import com.ruoyi.project.wechatprogram.wxcategory.domain.WxCategory;
import com.ruoyi.project.wechatprogram.wxcategory.service.IWxCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类目Controller
 * 
 * @author csx
 * @date 2020-03-27
 */
@Controller
@RequestMapping("/wechatprogram/webreport")
public class WxVisitDataController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(WxVisitDataController.class);

    private String prefix = "wechatprogram/webreport";

    @Autowired
    private IWxVisitDataService wxVisitDataService;

    @Autowired
    private IWxCategoryService wxCategoryService;

    @Autowired
    private IWxOrderMasterService wxOrderMasterService;

    @RequiresPermissions("wechatprogram:webreport:view")
    @GetMapping()
    public String visitData()
    {
        return prefix + "/visitData";
    }

    @RequiresPermissions("wechatprogram:webreport:viewDetail")
    @GetMapping("/visitData")
    public String detailData()
    {
        return prefix + "/detailData";
    }


    /**
     * 查询历史就餐人数
     *
     * @param startT
     * @param endT
     * @return
     */
    @RequiresPermissions("wechatprogram:webreport:list")
    @RequestMapping("/listVisitData")
    @ResponseBody
    public AjaxResult listVisitData(String startT, String endT)
    {
        startPage();
        List<WxVisitData> list = wxVisitDataService.selectWxVisitData(startT, endT);
        // 餐品类型
        WxCategory wxCategory = new WxCategory();
        wxCategory.setCategoryStatus(WxCategory.CATEGORY_STATUS_SHOW);
        List<WxCategory> wxCategoryList = wxCategoryService.selectWxCategoryList(wxCategory);
        Map<String, Object> params = new HashMap<>();
        params.put("list", list);
        params.put("categoryList", wxCategoryList);
        return AjaxResult.success(params);
    }

    /**
     * 查询实时点餐人数、菜品数、点餐人员详情记录
     *
     * @param time
     * @return
     */
    @RequiresPermissions("wechatprogram:webreport:list")
    @RequestMapping("/listDetailData")
    @ResponseBody
    public AjaxResult listDetailData(String time)
    {
        startPage();
        // 菜品数量查询
        List<WxVisitData> list = wxVisitDataService.selectWxDetailMenuData(time);
        // 餐品类型对应的数量
        List<WxVisitData> wxCategoryList = wxVisitDataService.selectWxDetailCategoryData(time);
        WxOrderMaster wxOrderMaster = new WxOrderMaster();
        wxOrderMaster.setOrderStatus(WxOrderMaster.ORDER_STATUS_WAIT);
        wxOrderMaster.setPayStatus(WxOrderMaster.PAY_STATUS_YES);
        wxOrderMaster.setDay(time);
        // 报表订单详情按天展示
        List<WxOrderMaster> listOrder = wxOrderMasterService.selectWxOrderMasterByDay(wxOrderMaster);
        TableDataInfo tableDataInfo = getDataTable(listOrder);
        Map<String, Object> params = new HashMap<>();
        params.put("menuList", list);
        params.put("categoryList", wxCategoryList);
        params.put("list", tableDataInfo);
        return AjaxResult.success(params);
    }
}
